/* This do-file generates the figures in the paper "The Rise of Star Firms: Intangible Capital and Competition" by Ayyagari, Maksimovic, and Demirguc-Kunt*/
/*See regfile_constrn.do for generating the data file regressionfile.dta used in this do-file and for the construction of the different variables*/

************************************************************
* FIGURE 1 - Rise in star firms: Conventional ROIC Metric
use regressionfile, clear 
xtset code year
keep if year>=1975
drop if at==. | sale==. 
drop if lag_investedcap_wogw2<0
g at_real=at/gdpdefl
g largeco=1 if at_real>=200 
keep if largeco==1
local xvar "ROIC_wogw2"
foreach v of local xvar{
bys year: egen med`v'=median(`v')
bys year: egen p25`v'=pctile(`v'), p(25)
bys year: egen p75`v'=pctile(`v'), p(75)
bys year: egen p90`v'=pctile(`v'), p(90)
}
keep year p25* med* p75* p90* 
duplicates drop
sort year
line p25ROIC_wogw2 year, lpattern("--..") || line medROIC_wogw2 year, lpattern(longdash) || line p75ROIC_wogw2 year, lpattern(shortdash) || line p90ROIC_wogw2 year, ///
legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1975(5)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Return on Invested Capital") subtitle("Subtracting Goodwill") ///
note("Conventional ROIC metric")

************************************************************
* FIGURE 2 - Differences in intangible intensity: Conventional ROIC metric
use regressionfile, clear 
xtset code year
keep if year>=1975
drop if at==. | sale==. 
drop if lag_investedcap_wogw2<0
local xvar "kint_inttan"
foreach v of local xvar{
qui summ `v', d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
}
bys naics3_real year: egen meankint=mean(kint_inttan_1d)
keep naics3_real year meankint
duplicates drop
g hi_kint=0
sort naics3_real year
forvalues i=1975(1)2015{
qui summ meankint if year==`i', d
replace hi_kint=1 if year==`i' & meankint>=r(p50) & meankint!=.
}
keep naics3_real year hi*
sort naics3_real year
save tbd1, replace

use regressionfile, clear 
xtset code year
drop if at==. | sale==. 
keep if year>=1975
drop if lag_investedcap_wogw2<0
g at_real=at/gdpdefl
g largeco=1 if at_real>=200 
keep if largeco==1
sort naics3_real year
merge m:1 naics3_real year using tbd1
tab _m
drop _m

local xvar "ROIC_wogw2"
foreach v of local xvar{
bys hi_kint year: egen med`v'=median(`v')
bys hi_kint year: egen p25`v'=pctile(`v'), p(25)
bys hi_kint year: egen p75`v'=pctile(`v'), p(75)
bys hi_kint year: egen p90`v'=pctile(`v'), p(90)
}
keep hi_kint year p25* med* p75* p90*
duplicates drop
sort year

line p25ROIC_wogw2 year if hi_kint==0, lpattern("--..") || line medROIC_wogw2 year if hi_kint==0, lpattern(longdash) || line p75ROIC_wogw2 year if hi_kint==0, lpattern(shortdash) ///
|| line p90ROIC_wogw2 year  if hi_kint==0, legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1975(5)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Low Intangible Intensity Industries") saving("x5.gph", replace) nodraw ylabel(0(50)150)

line p25ROIC_wogw2 year if hi_kint==1, lpattern("--..") || line medROIC_wogw2 year if hi_kint==1, lpattern(longdash) || line p75ROIC_wogw2 year if hi_kint==1, lpattern(shortdash) ///
|| line p90ROIC_wogw2 year  if hi_kint==1, legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1975(5)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("High Intangible Intensity Industries") saving("x6.gph", replace) nodraw
grc1leg "x5.gph" "x6.gph", title("Return on Invested Capital") note("Conventional ROIC metric") 

************************************************************
* FIGURE 3 - Rise in star firms: ROIC adjusted for intangible capital AND
* FIGURE 15 - Rise in star firms: Correcting for R&D capital versus SG&A capital
use regressionfile, clear 
xtset code year
keep if year>=1990
drop if at==. | sale==. 
drop if flag==1 /**i.e. Drop if ppegt_real<5**/
g icflag2=1 if lag_investedcap_wogw3>=0 & lag_investedcap_wogw3!=.
keep if icflag2==1 
local xvar "ROIC_wogw3 ROIC_wogw3_rd ROIC_wogw3_sga"
foreach v of local xvar{
qui summ `v' if icflag2==1, d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
bys year: egen med`v'=median(`v'_1d)
bys year: egen p25`v'=pctile(`v'_1d), p(25)
bys year: egen p75`v'=pctile(`v'_1d), p(75)
bys year: egen p90`v'=pctile(`v'_1d), p(90)
}
keep year p25* med* p75* p90* 
duplicates drop
sort year
line p25ROIC_wogw3 year, lpattern("--..") || line medROIC_wogw3 year, lpattern(longdash) || line p75ROIC_wogw3 year, lpattern(shortdash) || line p90ROIC_wogw3 year, lcolor(magenta) ///
legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(0(20)100, angle(horizontal)) xtitle("") title("Return on Invested Capital") ///
subtitle("Includes PT (2017) correction for intangible capital")
* Figure 15
line p25ROIC_wogw3_rd year, lpattern("--..") || line medROIC_wogw3_rd year, lpattern(longdash) || line p75ROIC_wogw3_rd year, lpattern(shortdash) || line p90ROIC_wogw3_rd year, lcolor(magenta) ///
legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Return on Invested Capital") ///
subtitle("Includes Correction for just R&D capital")

line p25ROIC_wogw3_sga year, lpattern("--..") || line medROIC_wogw3_sga year, lpattern(longdash) || line p75ROIC_wogw3_sga year, lpattern(shortdash) || line p90ROIC_wogw3_sga year, lcolor(magenta) ///
legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Return on Invested Capital") ///
subtitle("Includes  Correction for just Organizational (SGA) capital")

************************************************************
* FIGURE 4 - Differences in intangible intensity: ROIC adjusted for intangible capital
* First create high vs low intangible intensity industries
use regressionfile, clear
keep if year>=1975
drop if at==. | sale==. 
drop if flag==1 /**i.e. Drop if ppegt_real<5**/
g icflag2=1 if lag_investedcap_wogw3>=0 & lag_investedcap_wogw3!=.
keep if icflag2==1 
local xvar "kint_inttan"
foreach v of local xvar{
qui summ `v', d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
}

bys naics3_real year: egen meankint=mean(kint_inttan_1d)
keep naics3_real year meankint
duplicates drop
g hi_kint=0
sort naics3_real year
forvalues i=1975(1)2015{
qui summ meankint if year==`i', d
replace hi_kint=1 if year==`i' & meankint>=r(p50) & meankint!=.
}
keep naics3_real year hi*
sort naics3_real year
label var hi_kint "1 if mean(kint_inttan_1d)>=r(p50) in each year"
save hilow_icapind, replace

use regressionfile, clear
keep if year>=1990
drop if at==. | sale==. 
drop if flag==1 /**i.e. Drop if ppegt_real<5**/
g icflag2=1 if lag_investedcap_wogw3>=0 & lag_investedcap_wogw3!=.
keep if icflag2==1 
local xvar "ROIC_wogw3"
foreach v of local xvar{
qui summ `v', d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
}
sort naics3_real year
merge m:1 naics3_real year using hilow_icapind
tab _m
drop if _m==2
drop _m

local xvar "ROIC_wogw3"
foreach v of local xvar{
bys hi_kint year: egen med`v'=median(`v')
bys hi_kint year: egen p25`v'=pctile(`v'), p(25)
bys hi_kint year: egen p75`v'=pctile(`v'), p(75)
bys hi_kint year: egen p90`v'=pctile(`v'), p(90)
}
keep hi_kint year p25* med* p75* p90*
duplicates drop
sort year

line p25ROIC_wogw3 year if hi_kint==0, lpattern("--..") || line medROIC_wogw3 year if hi_kint==0, lpattern(longdash) || line p75ROIC_wogw3 year if hi_kint==0, lpattern(shortdash) ///
|| line p90ROIC_wogw3 year  if hi_kint==0, legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Low Intangible Intensity Industries") saving("x5.gph", replace) nodraw
line p25ROIC_wogw3 year if hi_kint==1, lpattern("--..") || line medROIC_wogw3 year if hi_kint==1, lpattern(longdash) || line p75ROIC_wogw3 year if hi_kint==1, lpattern(shortdash) ///
|| line p90ROIC_wogw3 year  if hi_kint==1, legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("High Intangible Intensity Industries") saving("x6.gph", replace) nodraw
grc1leg "x5.gph" "x6.gph", title("Return on Invested Capital") subtitle("Includes PT (2017) correction for intangible capital")


************************************************************
*FIGURE 6 - Markups in the US Economy
use regressionfile, clear
keep if year>=1990
drop if at==. | sale==. 
drop if flag==1 /**i.e. Drop if ppegt_real<5**/
g icflag2=1 if lag_investedcap_wogw3>=0 & lag_investedcap_wogw3!=.
keep if icflag2==1 
local xvar "costshare_opexPT"
foreach v of local xvar{
qui summ `v' if icflag2==1, d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
bys year: egen med`v'=median(`v'_1d)
bys year: egen p25`v'=pctile(`v'_1d), p(25)
bys year: egen p75`v'=pctile(`v'_1d), p(75)
bys year: egen p90`v'=pctile(`v'_1d), p(90)
}
line p25 year, lpattern("--..") || line medcostshare_opexPT year, lpattern(longdash) || line p75 year, lpattern(shortdash) || line p90 year, lcolor(magenta) ///
legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Markups") ///
subtitle("Includes PT (2017) correction for intangible capital")

************************************************************
*FIGURE 7 - Markups in the US Economy - Differences in intangible intensity
use regressionfile, clear
keep if year>=1990
drop if at==. | sale==. 
drop if flag==1 /**i.e. Drop if ppegt_real<5**/
g icflag2=1 if lag_investedcap_wogw3>=0 & lag_investedcap_wogw3!=.
keep if icflag2==1 
local xvar "costshare_opexPT"
foreach v of local xvar{
qui summ `v', d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
}
sort naics3_real year
merge m:1 naics3_real year using hilow_icapind
tab _m
drop if _m==2
drop _m

local xvar "costshare_opexPT"
foreach v of local xvar{
bys hi_kint year: egen med`v'=median(`v'_1d)
bys hi_kint year: egen p25`v'=pctile(`v'_1d), p(25)
bys hi_kint year: egen p75`v'=pctile(`v'_1d), p(75)
bys hi_kint year: egen p90`v'=pctile(`v'_1d), p(90)
}
keep hi_kint year p25* med* p75* p90*
duplicates drop
sort year

line p25 year if hi_kint==0, lpattern("--..") || line medcostshare_opexPT year if hi_kint==0, lpattern(longdash) || line p75 year if hi_kint==0, lpattern(shortdash) ///
|| line p90 year  if hi_kint==0, legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Low Intangible Intensity Industries") saving("x5.gph", replace) nodraw
line p25 year if hi_kint==1, lpattern("--..") || line medcostshare_opexPT year if hi_kint==1, lpattern(longdash) || line p75 year if hi_kint==1, lpattern(shortdash) ///
|| line p90 year  if hi_kint==1, legend(label(1 "25th percentile") label(2 "median") label(3 "75th percentile") label(4 "90th percentile")) ///
xlabel(1990(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("High Intangible Intensity Industries") saving("x6.gph", replace) nodraw
grc1leg "x5.gph" "x6.gph", title("Markups") subtitle("Includes PT (2017) correction for intangible capital") 


************************************************************
*FIGURE 10: Product life cycle and intangible intensity
use regsub, clear
binsreg ROIC_wogw3_1d kint_inttan_1d,  xline(0.75) ytitle("ROIC") xtitle("Intangible Intensity") title("ROIC vs. Intensity") nodraw saving("x1.gph", replace)
binsreg age kint_inttan_1d, xline(0.75) ytitle("Age") xtitle("Intangible Intensity") title("Age vs. Intensity") nodraw saving("x2.gph", replace)
binsreg SALES_IC_1d kint_inttan_1d, xline(0.75) ytitle("Sales/IC")  xtitle("Intangible Intensity") title("Sales/Invested Capital vs. Intensity") nodraw saving("x3.gph", replace)
binsreg life1 kint_inttan_1d, xline(0.75) ytitle("Life1") xtitle("Intangible Intensity") title("Life1 vs. Intensity") nodraw saving("x4.gph", replace)
grc1leg "x1.gph" "x2.gph" "x3.gph" "x4.gph"


************************************************************
*FIGURE 11: TFP, Investment, and Output
use regsub, clear
* Merge with productivity data
sort code year
merge 1:1 code year using DLW_mkups
drop if _m==2
drop _m
local xvar "prod_opex"
foreach v of local xvar{
qui summ `v', d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
}
binsreg prod_opex_1d kint_inttan_1d LIC costshare_opexPT_1d Lage, absorb(naics3_real#year) by(ab90_ROIC_wogw3) ci(3,3) cb(3,3) nbins(20) dots(0,0) line(3,3) xtitle("Intensity") ytitle("TFP") title("TFP vs. Intensity") bylpatterns(dash solid) bycolors(green navy) saving("Fig11_tfp.gph", replace) note("Controls: Markups, Size, Age, Ind x Year fixed effects") 

binsreg capx_IC_1d kint_inttan_1d costshare_opexPT_1d LIC Lage, absorb(naics3_real#year) by(ab90_ROIC_wogw3) ci(3,3) cb(3,3) nbins(20) dots(0,0) line(3,3) xtitle("Intangible Intensity") ytitle("Capex/Invested Capital") title("Capex")  bycolors(green navy) saving("z2.gph", replace) nodraw  
binsreg xrd_IC_1d kint_inttan_1d costshare_opexPT_1d LIC Lage, absorb(naics3_real#year) by(ab90_ROIC_wogw3) ci(3,3) cb(3,3) nbins(20) dots(0,0) line(3,3) xtitle("Intangible Intensity") ytitle("R&D/Invested Capital") title("R&D Investment")  bycolors(green navy) saving("z3.gph", replace) nodraw  
binsreg sga_IC_1d kint_inttan_1d costshare_opexPT_1d LIC Lage, absorb(naics3_real#year) by(ab90_ROIC_wogw3) ci(3,3) cb(3,3) nbins(20) dots(0,0) line(3,3) xtitle("Intangible Intensity") ytitle("SG&A/Invested Capital") title("SG&A Investment")  bycolors(green navy) saving("z4.gph", replace) nodraw  
binsreg SALES_IC_1d kint_inttan_1d costshare_opexPT_1d LIC Lage, absorb(naics3_real#year) by(ab90_ROIC_wogw3) ci(3,3) cb(3,3) nbins(20) dots(0,0) line(3,3) xtitle("Intangible Intensity") ytitle("Sales/Invested Capital") title("Output")  bycolors(green navy) saving("z5.gph", replace) nodraw  
grc1leg "z2.gph" "z3.gph" "z4.gph" "z5.gph", note("Controls for Markups, Size, Age, Ind x Year fixed effects") title("Investment and Output vs. Intensity")

************************************************************
*FIGURES 12 AND 13: ELITE FIRMS
use regressionfile, clear
keep if year>=1990
drop if at==. | sale==. 
drop if flag==1 /**i.e. Drop if ppegt_real<5**/
keep if lag_investedcap_wogw3>0 & lag_investedcap_wogw3!=. 
local xvar "ROIC_wogw3 costshare_opexPT"
foreach v of local xvar{
qui summ `v', d
g `v'_1d=`v' if `v'>=r(p1) & `v'<=r(p99)
bys year: egen med`v'=median(`v'_1d)
bys year: egen p25`v'=pctile(`v'_1d), p(25)
bys year: egen p75`v'=pctile(`v'_1d), p(75)
bys year: egen p90`v'=pctile(`v'_1d), p(90)
}
keep year p25* medROIC* medcost* p75* p90* 
duplicates drop
sort year
save ROICgraphs_data, replace

use regressionfile, clear
keep if year>=1990
keep if tic=="AMZN" | tic=="GOOGL" | tic=="FB" | tic=="NFLX" | tic=="MSFT" | tic=="AAPL"
keep tic year ROIC_wogw3 costshare_opexPT 
reshape wide ROIC_wogw3 costshare_opexPT, i(year) j(tic) string
sort year
merge 1:1 year using ROICgraphs_data
tab _m
drop _m
sort year
save ROICgraphs_data, replace
* Figure 12 - ROIC of elite firms (Apple, Facebook, Amazon, Microsoft, Google)
keep if year>=2000
line ROIC_wogw3FB year, lpattern(shortdash) lwidth(medthick) || ///
line ROIC_wogw3AMZN year, lpattern(longdash) lwidth(medthick) || ///
line ROIC_wogw3MSFT year, lpattern(dash_3dot) lwidth(medthick) || ///
line ROIC_wogw3GOOGL year, lpattern(dash_dot) lwidth(medthick) || ///
line ROIC_wogw3AAPL year, lpattern(dash) lwidth(medthick) || ///
line p90ROIC_wogw3 year, lcolor(magenta) lwidth(medthick) ///
legend(label(1 "Facebook") label(2 "Amazon") label(3 "Microsoft") label(4 "Google(Alphabet)")  label(5 "Apple") label(6 "90th percentile")) ///
xlabel(2000(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Return on Invested Capital") ///
subtitle("Includes PT(2017) correction for intangible capital")

* Figure 13-Markups of elite firms (Apple, Facebook, Amazon, Microsoft, Google)
line costshare_opexPTFB year, lpattern(shortdash) lwidth(medthick) || ///
line costshare_opexPTAMZN year, lpattern(longdash) lwidth(medthick) || ///
line costshare_opexPTMSFT year, lpattern(dash_3dot) lwidth(medthick) || ///
line costshare_opexPTGOOGL year, lpattern(dash_dot) lwidth(medthick) || ///
line costshare_opexPTAAPL year, lpattern(dash) lwidth(medthick) || ///
line p90costshare_opexPT year, lcolor(magenta) lwidth(medthick) ///
legend(label(1 "Facebook") label(2 "Amazon") label(3 "Microsoft") label(4 "Google(Alphabet)")  label(5 "Apple") label(6 "90th percentile")) ///
xlabel(2000(1)2015, labsize(small) angle(vertical)) ylabel(, angle(horizontal)) xtitle("") title("Markups") ///
subtitle("Includes PT(2017) correction for intangible capital")


************************************************************
*FIGURE 8 - PERSISTENCE IN STAR STATUS
use regsub, clear
order code year ab90_ROIC_wogw3
drop if ab90_ROIC_wogw3==.
sort code year
xtset code year
bys code: g star_2year=1 if ab90_ROIC_wogw3==1 & F.ab90_ROIC_wogw3==1
bys code: g star_3year=1 if ab90_ROIC_wogw3==1 & F.ab90_ROIC_wogw3==1 & F2.ab90_ROIC_wogw3==1
bys code: g star_4year=1 if ab90_ROIC_wogw3==1 & F.ab90_ROIC_wogw3==1 & F2.ab90_ROIC_wogw3==1 & F3.ab90_ROIC_wogw3==1 
bys code: g star_5year=1 if ab90_ROIC_wogw3==1 & F.ab90_ROIC_wogw3==1 & F2.ab90_ROIC_wogw3==1 & F3.ab90_ROIC_wogw3==1 & F4.ab90_ROIC_wogw3==1
tab year ab90_ROIC_wogw3
tab year star_2year
tab year star_3year
tab year star_4year
tab year star_5year
/**** Plot the distributions above for the figure in the paper****/

/***Lemmon-Roberts-Zender Panel****/
use regsub, clear
egen roic4q = xtile(ROIC_wogw3_1d), by(year) nq(4)
egen mkup4q = xtile(costshare_opexPT_1d), by(year) nq(4)
g p=roic4q if year==1990
bys code: egen totp=total(p)
drop if totp==0 /*When the firm doesnt have 1990 obs*/
keep if year>=1990 & year<=1990+14
collapse (mean) ROIC_wogw3_1d, by(totp year)
g t=year-1990
g startyr=1990
drop year
order startyr t totp
sort t totp
save LRZ_panel, replace

forvalues y=1991(1)2015{
use regsub, clear
egen roic4q = xtile(ROIC_wogw3_1d), by(year) nq(4)
egen mkup4q = xtile(costshare_opexPT_1d), by(year) nq(4)
g p=roic4q if year==`y'
bys code: egen totp=total(p)
drop if totp==0 /*When the firm doesnt have starting obs in year `y'*/
keep if year>=`y' & year<=`y'+14
collapse (mean) ROIC_wogw3_1d, by(totp year)
g t=year-`y'
g startyr=`y'
drop year
order startyr t totp
sort t totp
append using LRZ_panel
save LRZ_panel, replace
}

lgraph ROIC_wogw3_1d t totp, err(sd) title("Persistence in Star Status") xtitle("Event time") ///
legend(label(1 "Low(1st Quartile)") label(2 "Medium(2nd Quartile)") label(3 "High(3rd Quartile)") label(4 "Highest(4th Quartile)") cols(2)) ///
xlabel(0(1)14) ytitle("Mean ROIC")

************************************************************
*FIGURE 8 - Persistence in Markups
/***Lemmon-Roberts-Zender Panel****/
use regsub, clear
egen roic4q = xtile(ROIC_wogw3_1d), by(year) nq(4)
egen mkup4q = xtile(costshare_opexPT_1d), by(year) nq(4)
g p=mkup4q if year==1990
bys code: egen totp=total(p)
drop if totp==0 /*When the firm doesnt have 1990 obs*/
keep if year>=1990 & year<=1990+14
collapse (mean) costshare_opexPT_1d, by(totp year)
g t=year-1990
g startyr=1990
drop year
order startyr t totp
sort t totp
save LRZ_panel_mkup, replace

forvalues y=1991(1)2015{
use regsub, clear
egen roic4q = xtile(ROIC_wogw3_1d), by(year) nq(4)
egen mkup4q = xtile(costshare_opexPT_1d), by(year) nq(4)
g p=mkup4q if year==`y'
bys code: egen totp=total(p)
drop if totp==0 /*When the firm doesnt have starting obs in year `y'*/
keep if year>=`y' & year<=`y'+14
collapse (mean) costshare_opexPT_1d, by(totp year)
g t=year-`y'
g startyr=`y'
drop year
order startyr t totp
sort t totp
append using LRZ_panel_mkup
save LRZ_panel_mkup, replace
}
lgraph costshare_opexPT_1d t totp, err(sd) title("Persistence in Markups") xtitle("Event time") ///
legend(label(1 "Low(1st Quartile)") label(2 "Medium(2nd Quartile)") label(3 "High(3rd Quartile)") label(4 "Highest(4th Quartile)") cols(2)) ///
xlabel(0(1)14) ytitle("Mean Costshares")

************************************************************
*FIGURE 9 - Persistence in Early Markups
use regsub, clear
drop if costshare_opexPT_1d==.
sort gvkey fyear
bys gvkey: g id=_n
drop if at==. | sale==. 
drop if flag==1 /**i.e. Drop if ppegt_real<5 which is restriction from Peters and Taylor**/
keep if icflag2==1
* Form portfolios in year 5
g iniyear=year if id==5

qui summ costshare_opexPT_1d, d
bys year: egen p90mkup=pctile(costshare_opexPT_1d), p(90)
g ab90_mkup=1 if costshare_opexPT_1d>=p90mkup & costshare_opexPT_1d!=.
replace ab90_mkup=0 if ab90_mkup==. & costshare_opexPT_1d!=.
egen mkup4q = xtile(costshare_opexPT_1d), by(id) nq(4)
save tbd1, replace

use tbd1, clear
g p=mkup4q if year==iniyear
replace p=5 if ab90_mkup==1 & year==iniyear
bys code: egen totp=total(p)
drop if totp==0 /*When the firm doesnt have starting obs in year `y'*/
order code fyear iniyear id p totp costshare_opexPT_1d 
keep if id>=5 & id<=20
collapse (mean) costshare_opexPT_1d, by(totp id)
sort id totp
lgraph costshare_opexPT_1d id totp, err(sd) title("Persistence in Early Markups") xtitle("Age since IPO") ///
legend(label(1 "Low(1st Quartile)") label(2 "Medium(2nd Quartile)") label(3 "High(3rd Quartile)") label(4 "Highest(4th Quartile)")) ///
xlabel(5(1)20) ytitle("Mean Costshares") saving(inimkp_age5_per.gph, replace)

